home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / BTV115.ARJ / EXAMPLE1.PAS < prev    next >
Pascal/Delphi Source File  |  1992-06-02  |  6KB  |  220 lines

  1. {$X+}
  2. {$V-}
  3. { EXAMPLE1.PAS - demonstrate file creation with multiple keys,
  4.                  string justification, error trapping,
  5.                  reading by key value, reading by file position
  6.                  updating records
  7.  
  8.   Requires Turbo Pascal version 6.0
  9. }
  10.  
  11. Uses
  12.   Crt,
  13.   Dos,
  14.   Btv;
  15.  
  16.  
  17. type
  18.   ErrorType = Object(ErrorDisplay)
  19.     Function    Display(Error     : Integer;
  20.                         ErrorMsg  : String;
  21.                         OpCode    : Byte;
  22.                         OpCodeMsg : String;
  23.                         FileName  : PathStr
  24.                         ): ErrorAction;             Virtual;
  25.   end;
  26.  
  27.  
  28. var
  29.   F           : BtrieveFile;
  30.   Buff        : record
  31.                   Name    : String[30];
  32.                   Number  : String[5];
  33.                   Comment : String[80];
  34.                 end;
  35.   Pos         : LongInt;
  36.   Name        : String[30];
  37.   Number      : String[5];
  38.   ErrHandler  : ErrorHandler;
  39.   ErrDisplay  : ErrorType;
  40.   Major       : Word;
  41.   Minor       : Word;
  42.   Flag        : Char;
  43.  
  44.  
  45. { Heres our error display object  }
  46. Function ErrorType.Display(Error     : Integer;
  47.                            ErrorMsg  : String;
  48.                            OpCode    : Byte;
  49.                            OpCodeMsg : String;
  50.                            FileName  : PathStr
  51.                            ): ErrorAction;
  52.   begin
  53.     ClrScr;
  54.     Writeln('Btrieve IO error for ' + FileName);
  55.     Writeln(Error,  ' - ', ErrorMsg);
  56.     Writeln(Opcode, ' - ', OpCodeMsg);
  57.     Writeln('Press any key ....');
  58.     ReadKey;
  59.     Display := erDone;  { just let the program continue }
  60.     ClrScr;
  61.   end;
  62.  
  63. begin
  64.   { first make a error display }
  65.   ErrDisplay.Init;
  66.   { make an error handler, it needs a display object  }
  67.   ErrHandler.Init(@ErrDisplay);
  68.  
  69.   ClrScr;
  70.   Writeln('Creating a file called TEST1.DAT');
  71.  
  72.   { init the file passing it the error handler and }
  73.   { address of our data buffer                     }
  74.   F.Init('TEST1.DAT', @ErrHandler, @Buff, SizeOf(Buff));
  75.  
  76.   { the first thing to do is define the keys  }
  77.   { key is name, it is an lString, modifiable, has duplicates }
  78.   { and is left justified and padded                          }
  79.   F.AddKeySegment(1, 31, bExtended + bDuplicates + bModifiable,
  80.                    bLstring, 0, bLJustify);
  81.   { key is number, it is an lString, and is right justified }
  82.   F.AddKeySegment(32, 6, bExtended, bLstring, 0, bRJustify);
  83.   { now that all the keys are defined lets create and open it             }
  84.   { it will have no special features, but will overwrite any existing one }
  85.   F.Create(bNormal, SizeOf(Buff), 1024, 0, bNormal);
  86.   F.Open(bNormal, '');
  87.  
  88.   { lets add a couple records  }
  89.   Buff.Name   := 'AAAAAAAAAA';
  90.   Buff.Number := '1';   { the object will right justify this  }
  91.   Buff.Comment:= 'Record #1';
  92.   F.Insert;
  93.   Write('Adding some records .');
  94.   Delay(500);
  95.  
  96.   Buff.Name   := 'BBBBBBBBBB';
  97.   Buff.Number := '2';
  98.   Buff.Comment:= 'Record #2';
  99.   F.Insert;
  100.   Write('.');
  101.   Delay(500);
  102.  
  103.   Buff.Name   := 'CCCCCCCCCC';
  104.   Buff.Number := '3';
  105.   Buff.Comment:= 'Record #3';
  106.   F.Insert;
  107.   Write('.');
  108.   Delay(500);
  109.  
  110.   Buff.Name   := 'DDDDDDDDDD';
  111.   Buff.Number := '4';
  112.   Buff.Comment:= 'Record #4';
  113.   F.Insert;
  114.   Write('.');
  115.   Delay(500);
  116.  
  117.   Buff.Name   := 'EEEEEEEEEE';
  118.   Buff.Number := '5';
  119.   Buff.Comment:= 'Record #5';
  120.   F.Insert;
  121.   Writeln('.');
  122.  
  123.   { let's see how big the file is }
  124.   Writeln('There are ', F.NumberOfRecords, ' records in the file.');
  125.   Writeln('Press a key...');
  126.   ReadKey;
  127.  
  128.   Writeln('Reading by key, should generate an error #4 and test error trapping');
  129.   Writeln('Press a key...');
  130.   ReadKey;
  131.   { remember keys start at zero }
  132.   F.SetKeyPath(1);
  133.   { build the key, we'll try out the error handler and won't set up a match }
  134.   Number  :=  '9';  { no need to right justify this }
  135.   F.MakeKey(@Number, nil, nil, nil, nil, nil);
  136.   { read it without locks }
  137.   F.Get(bGetEqual, bNoLock);
  138.  
  139.   { okay no we'll remove error key not found from from the trapped set }
  140.   { and handle it ourselves                                            }
  141.   Writeln('Reading by the second key again, this time without error trapping');
  142.   ErrHandler.RemoveErrors([bKeyNotFound]);
  143.   F.Get(bGetEqual, bNoLock);
  144.   Writeln('File status = ', F.bResult);
  145.   Writeln('Press a key...');
  146.   ReadKey;
  147.  
  148.   { put key not found back in }
  149.   ErrHandler.AddErrors([bKeyNotFound]);
  150.   { try a key that should be in the file }
  151.   Writeln('Reading by the second key yet again');
  152.   Number  :=  '3';
  153.   F.MakeKey(@Number, nil, nil, nil, nil, nil);
  154.   F.Get(bGetEqual, bNoLock);
  155.  
  156.   if (F.bResult = bOkay) then
  157.   begin
  158.     Writeln('I think I got one');
  159.   end
  160.  
  161.   else
  162.   begin
  163.     Writeln('Something is wrong?');
  164.     Halt;
  165.   end;
  166.  
  167.   { read and save the current file position }
  168.   Pos := F.GetPosition;
  169.   Writeln(Buff.Name);
  170.   Writeln(Buff.Number);
  171.   Writeln(Buff.Comment);
  172.   Writeln('Current file positioning is ', Pos);
  173.   Writeln('Press a key...');
  174.   ReadKey;
  175.  
  176.   { Change it and write it back out }
  177.   Writeln('Changing that last record');
  178.   Buff.Comment := 'THIS RECORD HAS BEEN UPDATED!';
  179.   F.Update;
  180.   Writeln('Press a key...');
  181.   ReadKey;
  182.  
  183.   { read and display a record by position and see that it changed }
  184.   Writeln('Reading that last record by position');
  185.   Writeln('Here is a record in the file at position ', F.GetPosition);
  186.   F.GetDirect(bNoLock, Pos);
  187.   Writeln(Buff.Name);
  188.   Writeln(Buff.Number);
  189.   Writeln(Buff.Comment);
  190.   Writeln('Press a key...');
  191.   ReadKey;
  192.  
  193.   { read and display the next record  }
  194.   F.Get(bGetNext, bNoLock);
  195.   Writeln('Here is the next record in the file');
  196.   Writeln(Buff.Name);
  197.   Writeln(Buff.Number);
  198.   Writeln(Buff.Comment);
  199.   Writeln('Press a key...');
  200.   ReadKey;
  201.  
  202.   { read and display the first record  }
  203.   Writeln('Here is the first record in the file');
  204.   F.Get(bGetFirst, bNoLock);
  205.   Writeln(Buff.Name);
  206.   Writeln(Buff.Number);
  207.   Writeln(Buff.Comment);
  208.   Writeln('Press a key...');
  209.   ReadKey;
  210.  
  211.  
  212.   { Show the btrieve version }
  213.   F.Version(Major, Minor, Flag);
  214.   Writeln('You are running BTRIEVE version ', Major, '.', Minor, ' ', Flag);
  215.   Writeln('Press a key...');
  216.   ReadKey;
  217.  
  218.  
  219.   F.Close;
  220. end.